<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>卡片名称</span>
      </div>
      <div>
        <el-form
          :model="ruleFormData"
          :rules="rules"
          ref="ruleFormData"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="登录名称" prop="username">
            <el-input :disabled="true" v-model="ruleFormData.username"></el-input>
          </el-form-item>
          <el-form-item label="名称修改为" prop="username">
            <el-input v-model="ruleFormData.username"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleFormData')">立即修改</el-button>
            <el-button @click="resetForm('ruleFormData')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
<script>
import { resetusername } from "@/api/user";
export default {
  name: "myBaseData",
  data() {
    return {
      ruleFormData: {
        username: "NaN"
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 4, max: 8, message: "长度在 4 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.ruleFormData = JSON.parse(localStorage.getItem("userInfo"));
  },
  methods: {
    submitForm(ruleFormData) {
      this.$refs[ruleFormData].validate(valid => {
        if (valid) {
          this.$confirm("是否修改", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then( async () => {
              const data = {"username":this.ruleFormData.username,"id":this.ruleFormData.id}
              await resetusername(data).then(res =>{
                // 存入vuex
                this.$store.commit('UserInfo/SETUSERINFO',res.data.userInfo)
                 // 存入本地
                localStorage.setItem('userInfo',JSON.stringify(res.data.userInfo))
                location.reload()
              }).catch(error=>{
                this.$message({
                type: "info",
                message: error
              });
              })
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除"
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(ruleFormData) {
      this.$refs[ruleFormData].resetFields();
    }
  }
};
</script>